<?php

class SearchBooksController extends Controller
{
	public $searchedValue = '';

	public function actionIndex()
	{
//		$this->render('index');
//		$model = new Search();
//		$dataProvider=new CActiveDataProvider('Book', array ('pagination'=>array('pageSize'=>5)));
//		$this->render('index',array(
//			'dataProvider'=>$dataProvider,
//			'model'=>$model,
//		));
		return $this->actionSearch();
	}

	public function actionSearch()
	{
    	$model = new Search();

    	// uncomment the following code to enable ajax-based validation
    	/*
    	if(isset($_POST['ajax']) && $_POST['ajax']==='book-search-form')
    	{
        	echo CActiveForm::validate($model);
        	Yii::app()->end();
    	}
    	*/

    	if(isset($_POST['Search']))
    	{
        	$model->attributes=$_POST['Search'];
        	$model->searchingValue = trim($model->searchingValue);
        	if( strlen($model->searchingValue)>0) {
				$dataProvider=new CActiveDataProvider('Book',
					array ('criteria'=>array(
						'condition'=>'(title LIKE :title) OR (author LIKE :title)',
						'params'=> array(':title'=>'%'.$model->searchingValue.'%')),
						'pagination'=>array('pageSize'=>5)) );
        	} 
        	else {
				$dataProvider=new CActiveDataProvider('Book', array ('pagination'=>array('pageSize'=>5)));
    		}
		}
    	else {
			$dataProvider=new CActiveDataProvider('Book', array ('pagination'=>array('pageSize'=>5)));
   		}

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
			'model'=>$model,
		));

	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}